/**
 * 代理模式 
 */
//////  1
// 明星
let star = {
    name: '张XX',
    age: 25,
    phone: '13965652222'
}

// 经纪人
let agent = new Proxy(star, {
    get: function(target, key){
        if(key === 'phone'){
            //返回经纪人自己的电话
            return '13966666666'
        }
        if(key === 'price'){
            //明星不报价，经纪人才报价
            return 120000
        }
        return target[key]
    },
    set: function(target, key, value){
        if(key === 'customPrice') {
            if(value < 100000) {
                //报错
                throw new Error("价格太低")
            }else{
                target[key] = value;
                return true;
            }
        }
    }
})

//test
console.log(agent.name)
console.log(agent.age)
console.log(agent.phone)
console.log(agent.price)

agent.customPrice = 1000000;
console.log(agent)




/////   2
 // class RealImg {
//     constructor (fileName){
//         this.fileName = fileName;
//         this.loadFromDisk();
//     }
//     display() {
//         console.log("display..." + this.fileName)
//     }
//     loadFromDisk() {
//         console.log("loadFromDisk..." + this.fileName)
//     }
// }

// class ProxyImg {
//     constructor(fileName) {
//         this.realImg = new RealImg(fileName);
//     }
//     display() {
//         this.realImg.display();
//     }
// }

// //test
// let proxyImg = new ProxyImg("1.png");
// proxyImg.display();